<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;
use mdm\admin\components\Configs;
use mdm\admin\components\Helper;
use mdm\admin\models\AuthItem;
use mdm\admin\models\Route;
use mdm\admin\models\searchs\AuthItem as AuthItemSearch;
use yii\helpers\ArrayHelper;

class QxRouteController extends \mdm\admin\controllers\RouteController
{

    /**
     * {@inheritdoc}
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
        date_default_timezone_set('PRC');//设置时间时区为中国
        header("Content-type:application/x-www-form-urlencoded text/html; charset=utf-8");//设置格式为utf-8
        header('Access-Control-Allow-Origin:'.\Yii::$app->components['allowOutHttp']);
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        header('Access-Control-Allow-Headers:X-Requested-With, origin, content-type, accept');
        header('Access-Control-Allow-Credentials: true');
        return true;
    }

    /**
     * 将数据打包成 layui可以识别的数据格式
     * @param $count
     * @param $all
     * @return array
     */
    public function packData($count,$all){
        $res = [];
        $res['code'] = 0;
        $res['msg'] = "";
        $res['count'] = $count;
        $res['data'] = $all;
        return json_encode((object)$res);
    }

    /**
     * 将数据处理成为前端bootstraper可以识别的数据
     */
    public function dealData($opts){
        //将数据处理成前端可以使用的数据
        $optsDeal = [];
        if (count($opts)>1){
            if (count($opts['available'])==0){
                $optsDeal['available'] = $opts['available'];
            }
            foreach ($opts['available'] as $k=>$v){//对未分配的角色进行筛选
                $optsDeal['available'][$k]['value'] = $v;
                $optsDeal['available'][$k]['text'] = $v;
            }
            if (count($opts['assigned'])==0){
                $optsDeal['assigned'] = $opts['assigned'];
            }
            foreach ($opts['assigned'] as $k=>$v){//对已经分配的角色进行筛选
                $optsDeal['assigned'][$k]['value'] = $v;
                $optsDeal['assigned'][$k]['text'] = $v;
            }
        }
        return  $optsDeal;
    }

    /**
     * Lists all Route models.
     * @return mixed
     */
    public function actionList()
    {
        $model = new Route();
        $routes = $model->getRoutes();
        $res['data'] = $routes;
        $res['res'] = 1;
        return json_encode($res);
    }

    /**
     * Assign routes
     * @return array
     */
    public function actionAssign()
    {
        $routes = \Yii::$app->getRequest()->post('routes', []);
        $res = [];
        if (empty($routes[0])){//没有选择角色
            $res['data'] = "请选择角色";
            $res['res'] = 0;
            return json_encode($res);
        }
        $model = new Route();
        $model->addNew($routes);
        $opts = @$model->getRoutes();//拿到所有分配完成后的数据
        $optsDeal = $this->dealData($opts);
        $res['data'] = $optsDeal;
        $res['routes'] = $routes;
        $res['res'] = 1;
        return json_encode($res);
    }

    /**
     * Remove routes
     * @return array
     */
    public function actionRemove()
    {
        $routes = \Yii::$app->getRequest()->post('routes');
        $res = [];
        if (empty($routes[0])){//没有选择角色
            $res['data'] = "请选择角色";
            $res['res'] = 0;
            return json_encode($res);
        }
        $model = new Route();
        $model->remove($routes);
        $opts = @$model->getRoutes();//拿到所有分配完成后的数据
        $optsDeal = $this->dealData($opts);
        $res['data'] = $optsDeal;
        $res['routes'] = $routes;
        $res['res'] = 1;
        return json_encode($res);
    }


}